package com.example.home

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.base.base.activity.BaseActivity
import com.example.base.base.activity.BaseVBActivity
import com.example.home.bean.request.Location
import com.example.home.databinding.ActivityHomeBinding
import com.example.home.viewmodel.*
import com.therouter.TheRouter
import com.therouter.router.Route
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map

@Route(path = "http://bdnrc.org.cn/home", description = "地图页")
class HomeActivity : BaseVBActivity<HomeViewModel, ActivityHomeBinding>() {
    companion object{
        private const val TAG = "HomeActivity"
    }
    override fun init() {
        mBinding.gotoMyPageButton.setOnClickListener{
            mViewModel.navigationToMyPage("6")
        }

        mBinding.fetchData.text = "点击获取数据"
        mBinding.fetchData.setOnClickListener {
            Log.v(TAG, "点击获取数据被点击")
            mViewModel.sendUiIntent(HomeIntent.RequestMarkerList(Location(39.992186, 116.330038)))
        }
        lifecycleScope.launchWhenStarted {
            mViewModel.uiStateFlow.map { it.markerUiState }
                .distinctUntilChanged()
                .collect{ markerState ->
                    when (markerState){
                        is MarkerUiState.INIT -> {}
                        is MarkerUiState.SUCCESS -> {
                            Log.v(TAG, "加载地图Marker成功")
                            mBinding.textView.text = markerState.markers.toString()
                        }
                    }
                }
        }

        lifecycleScope.launchWhenStarted {
            mViewModel.uiStateFlow.map { it.loadUiState }
                .distinctUntilChanged()
                .collect{ loadUiState ->
                    when (loadUiState){
                        is LoadUiState.Idle -> { Log.v(TAG, "LoadUiState 初始化中")}
                        is LoadUiState.Loading -> { Log.v(TAG, "LoadUiState 是否加载：${loadUiState.isShow}")}
                        is LoadUiState.ShowContent -> { Log.v(TAG, "LoadUiState 展示界面")}
                        is LoadUiState.Error -> { Log.v(TAG, "LoadUiState 出错：${loadUiState.msg}")}
                    }
                }
        }

        lifecycleScope.launchWhenResumed {
            // 一次性状态的接收与处理
            mViewModel.effectFlow
                .collect{ homeEffect ->
                    when (homeEffect){
                        is HomeEffect.NavigationMyPage -> {
                            // 跳转到我的主页
                            TheRouter.build("http://bdnrc.org.cn/mypage")
                                .withBoolean("keyBoolean", true)
                                .withString("keyString", "123345")
                                .navigation(this@HomeActivity)
                        }
                    }
                }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.v(TAG, "HomeActivity onCreate()...")
    }

    override fun onRestart() {
        super.onRestart()
        Log.v(TAG, "HomeActivity onRestart()...")

    }

    override fun onStart() {
        super.onStart()
        Log.v(TAG, "HomeActivity onStart()...")

    }

    override fun onPause() {
        super.onPause()
        Log.v(TAG, "HomeActivity onPause()...")

    }

    override fun onResume() {
        super.onResume()
        Log.v(TAG, "HomeActivity onResume()...")

    }

    override fun onStop() {
        super.onStop()
        Log.v(TAG, "HomeActivity onStop()...")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(TAG, "HomeActivity onDestroy()...")

    }

    override fun onBackPressed() {
        // home页面返回到桌面
        // 创建一个意图，模仿用户按下Home键的行为
        val startMain = Intent(Intent.ACTION_MAIN)
        startMain.addCategory(Intent.CATEGORY_HOME)
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(startMain)
    }

}